package com.hdjk.view.util;

import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import com.lst.projectlib.animation.SimpleAnimationListener;

/**
 * Created by Administrator on 2017/3/9.
 */

public class AnimUtil {

    public static void shakeViewAnim(final View view, int times) {
        final int timeNow = times - 1;
        final TranslateAnimation animleft = new TranslateAnimation(0, -5, 0, 0);
        animleft.setInterpolator(new AccelerateDecelerateInterpolator());
        animleft.setDuration(20);
        animleft.setRepeatMode(Animation.REVERSE);
        animleft.setRepeatCount(1);
        animleft.setAnimationListener(new SimpleAnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                TranslateAnimation animRight = new TranslateAnimation(0, 5, 0, 0);
                animRight.setInterpolator(new AccelerateDecelerateInterpolator());
                animRight.setDuration(20);
                animRight.setRepeatMode(Animation.REVERSE);
                animRight.setRepeatCount(1);
                animRight.setAnimationListener(new SimpleAnimationListener() {
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        if (timeNow > 0) {
                            shakeViewAnim(view, timeNow);
                        }
                    }
                });

                view.startAnimation(animRight);
            }
        });

        view.startAnimation(animleft);
    }
}
